import { Collection as L } from '../nodes/Collection.js';
import { isNode as C, isPair as j } from '../nodes/identity.js';
import { stringify as P } from './stringify.js';
import { lineComment as F, indentComment as b } from './stringifyComment.js';
function D(c, r, o) {
  return (r.inFlow ?? c.flow ? O : N)(c, r, o);
}
function N(
  { comment: c, items: r },
  o,
  {
    blockItemPrefix: u,
    flowChars: m,
    itemIndent: h,
    onChompKeep: d,
    onComment: y,
  },
) {
  const {
      indent: $,
      options: { commentString: B },
    } = o,
    v = Object.assign({}, o, { indent: h, type: null });
  let e = !1;
  const f = [];
  for (let i = 0; i < r.length; ++i) {
    const l = r[i];
    let a = null;
    if (C(l))
      !e && l.spaceBefore && f.push(''),
        k(o, f, l.commentBefore, e),
        l.comment && (a = l.comment);
    else if (j(l)) {
      const n = C(l.key) ? l.key : null;
      n && (!e && n.spaceBefore && f.push(''), k(o, f, n.commentBefore, e));
    }
    e = !1;
    let s = P(
      l,
      v,
      () => (a = null),
      () => (e = !0),
    );
    a && (s += F(s, h, B(a))), e && a && (e = !1), f.push(u + s);
  }
  let t;
  if (f.length === 0) t = m.start + m.end;
  else {
    t = f[0];
    for (let i = 1; i < f.length; ++i) {
      const l = f[i];
      t += l
        ? `
${$}${l}`
        : `
`;
    }
  }
  return (
    c
      ? ((t +=
          `
` + b(B(c), $)),
        y && y())
      : e && d && d(),
    t
  );
}
function O(
  { comment: c, items: r },
  o,
  { flowChars: u, itemIndent: m, onComment: h },
) {
  const {
    indent: d,
    indentStep: y,
    flowCollectionPadding: $,
    options: { commentString: B },
  } = o;
  m += y;
  const v = Object.assign({}, o, {
    indent: m,
    inFlow: !0,
    type: null,
  });
  let e = !1,
    f = 0;
  const t = [];
  for (let s = 0; s < r.length; ++s) {
    const n = r[s];
    let g = null;
    if (C(n))
      n.spaceBefore && t.push(''),
        k(o, t, n.commentBefore, !1),
        n.comment && (g = n.comment);
    else if (j(n)) {
      const p = C(n.key) ? n.key : null;
      p &&
        (p.spaceBefore && t.push(''),
        k(o, t, p.commentBefore, !1),
        p.comment && (e = !0));
      const S = C(n.value) ? n.value : null;
      S
        ? (S.comment && (g = S.comment), S.commentBefore && (e = !0))
        : n.value == null && p != null && p.comment && (g = p.comment);
    }
    g && (e = !0);
    let w = P(n, v, () => (g = null));
    s < r.length - 1 && (w += ','),
      g && (w += F(w, m, B(g))),
      !e &&
        (t.length > f ||
          w.includes(`
`)) &&
        (e = !0),
      t.push(w),
      (f = t.length);
  }
  let i;
  const { start: l, end: a } = u;
  if (t.length === 0) i = l + a;
  else if (
    (e ||
      (e =
        t.reduce((n, g) => n + g.length + 2, 2) >
        L.maxFlowStringSingleLineLength),
    e)
  ) {
    i = l;
    for (const s of t)
      i += s
        ? `
${y}${d}${s}`
        : `
`;
    i += `
${d}${a}`;
  } else i = `${l}${$}${t.join(' ')}${$}${a}`;
  return c && ((i += F(i, d, B(c))), h && h()), i;
}
function k({ indent: c, options: { commentString: r } }, o, u, m) {
  if ((u && m && (u = u.replace(/^\n+/, '')), u)) {
    const h = b(r(u), c);
    o.push(h.trimStart());
  }
}
export { D as stringifyCollection };
